﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Transport.Admin
{
    public partial class QLThongTinDatXe : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btn_Calculate_Click(object sender, EventArgs e)
        {
            try
            {
                if (GridView_Resrv.Rows.Count != 0)
                {
                    ChiTietDatXeControl ctrl = new ChiTietDatXeControl();

                    foreach (GridViewRow row in GridView_Resrv.Rows)
                    {
                        CheckBox cb = (CheckBox)row.FindControl("cb_Select");
                        if (cb != null && cb.Checked)
                        {
                            string id = GridView_Resrv.Rows[row.RowIndex].Cells[2].Text;
                            int fee = ctrl.calculateFee(id);
                            ctrl.updatePhiDuKien(id, fee);
                        }
                    }
                }

                Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
            }
            catch (Exception)
            {
            }
        }

        protected void btn_CreateID_Click(object sender, EventArgs e)
        {
            DateTime centuryBegin = new DateTime(2000, 1, 1);
            DateTime currentDate = DateTime.Now;

            txt_ResvrID.Text = (currentDate.Ticks - centuryBegin.Ticks).ToString();
        }

        protected void btn_Confirm_Click(object sender, EventArgs e)
        {
            try
            {
                ChiTietDatXeControl ctrl = new ChiTietDatXeControl();

                foreach (GridViewRow row in GridView_Resrv.Rows)
                {
                    CheckBox cb = (CheckBox)row.FindControl("cb_Select");
                    if (cb != null && cb.Checked)
                    {
                        string id = GridView_Resrv.Rows[row.RowIndex].Cells[2].Text;
                        ctrl.updateReservationStatus(id, "2");
                        ctrl.updateTienDaThanhToan(id , Int16.Parse(GridView_Resrv.Rows[row.RowIndex].Cells[18].Text));
                    }
                }

                Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
            }
            catch (Exception)
            {

            }
        }

        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            try
            {
                ChiTietDatXeControl ctrl = new ChiTietDatXeControl();

                foreach (GridViewRow row in GridView_Resrv.Rows)
                {
                    CheckBox cb = (CheckBox)row.FindControl("cb_Select");
                    if (cb != null && cb.Checked)
                    {
                        string id = GridView_Resrv.Rows[row.RowIndex].Cells[2].Text;

                        ctrl.cancelReservation(id);
                    }
                }

                Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
            }
            catch (Exception)
            {

            }
        }

        protected void DetailsView_Rersv_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            GridView_Resrv.DataBind();
        }
    }
}